﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DragonSolution.Web.Common.Common
{
    public class CommonUtil
    {
        /// <summary>
        /// 分页通用类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class SpiltPageList<T> 
        {
            //页索引
            public int PageIndex { get; set; }
            //页大小
            public int PageSize { get; set; }
            //总数
            public int TotalCount { get; set; }
            //总页数
            public int TotalPages { get; set; }

            public List<T> DataList { get; set; }

            /// <summary>
            /// 构造方法
            /// </summary>
            /// <param name="source">数据源</param>
            /// <param name="pageIndex">页索引</param>
            /// <param name="pageSize">页大小</param>
            public SpiltPageList(IQueryable<T> source, int pageIndex, int pageSize)
            {
                PageIndex = pageIndex;
                PageSize = pageSize;
                TotalCount = source.Count();
                TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
                DataList = new List<T>();
                DataList.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
            }

            public bool HasPreviousPage
            {
                get
                {
                    return (PageIndex > 0);
                }
            }

            public bool HasNextPage
            {
                get
                {
                    return (PageIndex + 1 < TotalPages);
                }
            }
        }           
    }
}
